package com.pfizer.Utils;

import java.util.List;
import java.util.Map;

import com.pfizer.pfizerExpertForPad.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
 * SurveyListAdapter--<b>問卷調查信息的數據適配器，用於綁定listview</b>--
 * 
 * @see android.widget.BaseAdapter
 * @author krui
 * @version 1.0 2011/11/24
 */
public class SurveyListAdapter extends BaseAdapter {
	List<Map<String, Object>> list;//定義一個list得到數據源
	Context mContext;//上下文對象
	private int selectItem = 0; // 定义初始选定位置
	
	public SurveyListAdapter() {
		// TODO Auto-generated constructor stub
	}
/**
 * 構造函數
 * @param list
 * @param mContext
 */
	public SurveyListAdapter(List<Map<String, Object>> list,
			Context mContext) {
		this.list = list;
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		SurveyViewHolder vh = null;
		if (convertView == null) {
			vh = new SurveyViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.surveylistitem, null);//從佈局文件中過濾出佈局
			vh.tvBgTime = (TextView)convertView.findViewById(R.id.tvBgTime);
			vh.tvTitle = (TextView)convertView.findViewById(R.id.tvTitle);
			vh.tvCont = (TextView)convertView.findViewById(R.id.tvCont);
			convertView.setTag(vh);//設置tag用於緩存
		}else {
			vh = (SurveyViewHolder) convertView.getTag();//如果緩存中友實例就不需要再去得到每一個控件的實例
		}
		
		//写数据
		vh.tvBgTime.setText((String)list.get(position).get("BgTime"));
		vh.tvTitle.setText((String)list.get(position).get("Title"));
		vh.tvCont.setText((String)list.get(position).get("Cont"));
		
		// 更换Item背景
		if (position == selectItem) {
			convertView.setBackgroundResource(R.drawable.surveylist_bg_over);
		} else {
			convertView.setBackgroundResource(R.drawable.surveylistitem_bg);
		}
		
		return convertView;
	}
	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}
	public int getSelectItem() {
		return this.selectItem;
	}
}
